package com.jay.nio.clazz;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.Arrays;

public class FloatBufferCls {
	public static void main(String[] args) throws IOException {
		// 创建一个capacity为256的DoubleBuffer
		FloatBuffer buf = FloatBuffer.allocate(256);
		System.out.println("is direct? " + buf.isDirect());
		System.out.println(buf.array().length);
		System.out.println(buf.hasArray());
		System.out.println(buf.isReadOnly());
		System.out.println(buf.order());

		System.out.println("-----------------------------");
		buf.put(1.01f);
		buf.put(2.02f);
		buf.put(3.03f);
		buf.put(4.04f);
		buf.put(5.05f);
		buf.put(6.06f);
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		buf.flip();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		float[] content = new float[buf.limit()];
		buf.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(Arrays.toString(content));
		System.out.println();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		System.out.println(buf.arrayOffset());

		buf.clear();

		System.out.println("-----------------------------");
		buf.put(1.1f);
		buf.put(2.2f);
		// rewind 不重设limit，所以以前剩余的字符也会输出
		// buf.rewind();
		buf.flip();
		content = new float[buf.limit()];
		buf.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(Arrays.toString(content));
		System.out.println();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		buf.clear();
		System.out.println("-----------------------------");
		buf.position(2);
		buf.put(9.9f);
		buf.put(8.8f);
		buf.put(7.7f);
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		buf.flip();
		buf.position(2);
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		// 比较复杂，可参见实现代码
		buf.compact();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		System.out.println("-----------------------------");
		// 复制
		FloatBuffer bufDup = buf.duplicate();
		System.out.println(bufDup.position() + "/" + bufDup.limit() + "/"
				+ bufDup.capacity());

		// position后面的子buff
		FloatBuffer bufSlice = buf.slice();
		System.out.println(bufSlice.position() + "/" + bufSlice.limit() + "/"
				+ bufSlice.capacity());

		// position后面的子buff
		FloatBuffer bufWrap = FloatBuffer.wrap(new float[] { 94534.432f,
				423.4324f, 324.23f });
		System.out.println(bufWrap.position() + "/" + bufWrap.limit() + "/"
				+ bufWrap.capacity());

		System.out.println("-----------------------------");
		FloatBuffer bufD = ByteBuffer.allocateDirect(256).asFloatBuffer();
		System.out.println("is direct? " + bufD.isDirect());
		System.out.println(bufD.hasArray());
		System.out.println(bufD.isReadOnly());
		System.out.println(bufD.order());

		buf.put(1.01f);
		buf.put(2.02f);
		buf.put(3.03f);
		buf.put(4.04f);
		buf.put(5.05f);
		buf.put(6.06f);
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());
		bufD.flip();
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());

		content = new float[bufD.limit()];
		bufD.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(Arrays.toString(content));
		System.out.println();
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());

		bufD.clear();
	}
}
